<?php

class Register extends M_Controller
{

    // 注册验证码查询
    public function codeCheck()
    {
        if (empty($_GET['code']) || empty($_GET['phone'])) {
            x_json(0, '信息不完全');
        }
        $code = $this->models('system/sms')->getCaptcha(trim($_GET['phone']), 'register');
        if ($code == $_GET['code']) {
            x_json(1, '跳往下个页面..');
        } else {
            x_json(0, '验证码错误');
        }
    }

    /**
     *  MB注册 更新账号和密码
     * 请求登录类型的验证码
     */
    public function mbRegister()
    {
        try {
            $this->db->trans_begin();
            if ($_POST['password'] != $_POST['new_password']) {
                throw new Exception('两次密码不一样', 1);
            }
            if (!empty($_POST['invite'])) {
                $parentInfo = $this->models('member')->fieldInfo('invite_code', $_POST['invite']);
                if (empty($info)) {
                    throw new Exception('邀请码暂无对应用户', 1);
                }
            }
            $memberInfo = $this->models('member')->uidInfo($this->uid, 'uid,salt,password');

            if (md5(md5($_POST['password']) . $memberInfo['salt'] . md5($_POST['password'])) == $memberInfo['password']) {
                throw new Exception('与原密码相同', 1);
            }

            $data = [
                'password' => md5(md5(trim($_POST['password'])) . $memberInfo['salt'] . md5(trim($_POST['password']))),
                'invite_code' => $_POST['invite'] ?? '',
                'pid' => $parentInfo['uid'] ?? 0
            ];

            $rows = $this->models('member')->updateMember($data, $this->uid);
            if ($rows < 0) {
                throw new Exception('设置失败', 1);
            }
            $this->db->trans_complete();
            x_json(1, $this->models('member/login')->authcode($this->uid), '更新成功');
        } catch (Exception $e) {
            if ($e->getCode() == 1) {
                $this->db->trans_rollback();
            }
            x_json(0, $e->getMessage(), false);
        }
    }

    /**
     * PC 注册
     */
    public function PcRegister()
    {
        try {
            $this->db->trans_begin();
            $data = $this->validate($_POST, 'pc');
            if (!is_array($data)) {
                throw new Exception($data, 1);
            }
            $member_uid = $this->models('member/register')->add($data);
            if ($member_uid < 0) {
                throw  new Exception($this->models('member/register')->error_msg($member_uid), 1);
            }
            $this->models('system/sms')->deleteCaptcha(trim($data['phone']), 'register');
            $this->db->trans_complete();
            $this->input->set_cookie('member_uid', $member_uid, 86400);
            $this->input->set_cookie('member_cookie', substr(md5(SYS_KEY . '123456'), 5, 20), 86400);
            x_json(1, '注册成功正在跳转登录...');
        } catch (Exception $e) {
            if ($e->getCode() == 1) {
                $this->db->trans_rollback();
            }
            x_json(0, $e->getMessage(), false);
        }
    }


    /**
     * 注册验证
     * @param $data
     * @param $type
     * @return array|bool
     */
    private function validate($data, $type)
    {
        if (empty($data)) {
            return false;
        }
        $code = $this->models('system/sms')->getCaptcha(trim($data['phone']), 'register');

        if (empty($code)) {
            return '验证码不存在';
        }
        if ($code != $data['code']) {
            return '验证码不正确';
        }
        if (empty($data['phone'])) {
            return '手机号错误';
        }
        if (empty($data['password']) && $type = 'pc') {
            return '请输入密码';
        }
        if (empty($data['phone']) && $type = 'pc') {
            return '请输入账户名';
        }
        return [
            'password' => $data['password'] ?? '123456',
            'username' => $data['username'] ?? $data['phone'],
            'phone' => $data['phone'],
        ];
    }

}